﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_rkcl : Form
    {
        public gl_rkcl()
        {
            InitializeComponent();
        }

        public string rkfs;
        string comStr;
        #region 增加按钮
        private void button2_Click(object sender, EventArgs e)
        {
            gl_yprkd yprkd = new gl_yprkd();
            yprkd.Owner = this;
            rkfs = comboBox1.Text.ToString();
            yprkd.ShowDialog();
        }
        #endregion

        #region 刷新函数
        private void shuaxin()
        {
            DataTable dt4 = new DataTable();
            dt4.Clear();
            comStr = "select rkdh,rkbz from YF_RK01 where rkpb=0";
            dt4.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(dt4.Rows.Count);
            for (int i = 0; i < dt4.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = dt4.Rows[i][0].ToString();
                dataGridView1.Rows[i].Cells[1].Value = dt4.Rows[i][1].ToString();
            }
        }
        #endregion

        #region 显示左和右DatagridView
        private void xianshi_DatagridView()
        {
            //左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select rkdh,rkbz from YF_RK01 where rkpb=0";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(dt1.Rows.Count);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                dataGridView1.Rows[i].Cells[1].Value = dt1.Rows[i][1].ToString();
            }

            //右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select rkdh,rkrq,rkbz from YF_RK01 where rkpb=1";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            dataGridView2.Rows.Add(dt2.Rows.Count);
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                dataGridView2.Rows[i].Cells[0].Value = dt2.Rows[i][0].ToString();
                dataGridView2.Rows[i].Cells[1].Value = Convert.ToDateTime(dt2.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                dataGridView2.Rows[i].Cells[2].Value = dt2.Rows[i][2].ToString();
            }
        }
        #endregion

        #region 打开入库处理load
        private void gl_rkcl_Load(object sender, EventArgs e)
        {
            //入库方式
            int yfsb = 3;
            DataTable dt = new DataTable();
            comStr = "select fsmc from YF_RKFS where yfsb=" + yfsb + "";
            dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0]);
            }
            comboBox1.Text = dt.Rows[0][0].ToString();

            xianshi_DatagridView();
        }
        #endregion

        #region 删除按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除入库单"+""+dataGridView1.CurrentRow.Cells[0].Value.ToString()+""+"？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                try
                {
                    string comStr = "delete from YF_RK01 where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                    shuaxin();
                    MessageBox.Show("删除入库单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        #endregion  

        #region 刷新按钮
        private void button1_Click(object sender, EventArgs e)
        {
            shuaxin();
        }        
        #endregion

        #region 提交按钮
        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否提交入库单"+""+dataGridView1.CurrentRow.Cells[0].Value.ToString()+""+"？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                try
                {
                    comStr = "update YF_RK01 set rKPB='1' where rkdh='"+dataGridView1.CurrentRow.Cells[0].Value.ToString()+"'";
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                    xianshi_DatagridView();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        #endregion       
    }
}
